#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<wait.h>
#include<stdlib.h>

// 多进程版本
int main()
{
  printf("testexec ... begin!\n");
  pid_t id = fork();
  if(id == 0)
  {
    // child
    // 我的父进程本身就有一批环境变量，从bash来，自己想添加可以使用putenv
    putenv("HHHH=333333333");// 头文件 stdlib.h
    char* const argv[]=
    {
      (char*)"mypragma",
      (char*)"-a",
      (char*)"-b",
      NULL 
    };
    //char* const envp[]=
    //{
    //  (char*)"HAHA=111111",
    //  (char*)"HEHE=222222",
    //  NULL 
    //};
    printf("I am child process,pid:%d\n",getpid());
    sleep(2);
    extern char** environ;
    execvpe("./mypragma",argv,environ);
    //execvpe("./mypragma",argv,envp);

    //execl("/usr/bin/ls","ls","-a","-l",NULL);
    //execl("./mypragma","mypragma",NULL);
    //execl("/usr/bin/python3","phthon3","testpy",NULL);
    //execl("/usr/bin/bash","bash","testsh",NULL);
    //char* const argv[]=
    //{
    // (char*)"ls",
    // (char*)"-a",
    // (char*)"-l",
    // (char*)"--color",
    //  NULL
    //};
    //execv("/usr/bin/ls",argv);
    //execvp("ls",argv);
  }
  
  // father
  int status = 0;
  pid_t rid = waitpid(id,&status,0);
  if(rid > 0)
  {
    printf("father wait success,child exit code:%d\n",WEXITSTATUS(status));
  }
  else 
  {
    printf("father wait failed!\n");
  }
  printf("testexec ... end!\n");
  return 0;
}

//int main()
//{
//  printf("testexec ... begin!\n");
//  execl("/usr/bin/lsss","ls","-a","-l","--color",NULL);
//  printf("testexec ...  end!\n");
//  return 0;
//}
